package com.sunlee.common.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sunlee.common.service.dao.MarketingDao;
import com.sunlee.common.service.dto.MarketingDTO;
import com.sunlee.common.service.dto.ProductActionDTO;
import com.sunlee.common.service.entity.MarketingEntity;
import com.sunlee.common.service.entity.vo.CarouselProductVO;
import com.sunlee.common.service.entity.vo.CarouselVO;
import com.sunlee.common.service.service.MarketingProductMappingService;
import com.sunlee.common.service.service.MarketingService;
import com.sunlee.common.service.service.UserActionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 营销表
 *
 * @author sunlee
 */
@Service
public class MarketingServiceImpl extends CrudServiceImpl<MarketingDao, MarketingEntity, MarketingDTO> implements MarketingService {

    @Autowired
    private MarketingProductMappingService marketingProductMappingService;

    @Autowired
    private UserActionService userActionService;

    @Override
    public CarouselVO getCarousel(Integer type) {
        CarouselVO carouselVO = baseDao.selectetCarousel(type);
        if (carouselVO == null || CollectionUtils.isEmpty(carouselVO.getCarouselProducts())) {
            return null;
        }

        List<CarouselProductVO> carouselProducts = carouselVO.getCarouselProducts();
        List<Long> pIds = carouselProducts.stream().map(CarouselProductVO::getId).collect(Collectors.toList());
        Map<Long, ProductActionDTO> productMap = userActionService.getProductActionDataByIds(pIds);

        carouselProducts.forEach(item -> item.setActionData(productMap.get(item.getId())));

        return carouselVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRelByIds(Long[] mIds) {
        marketingProductMappingService.deleteByMIds(Arrays.asList(mIds));
        deleteOwn(mIds);
    }
}